package rentapartments.xwj.com.rentapartments.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.MyLocationStyle;

import me.yokeyword.fragmentation.ISupportFragment;
import me.yokeyword.fragmentation.SupportFragment;
import rentapartments.xwj.com.rentapartments.R;

/**
 * Created by thinkjoy on 2017/7/28.
 * 地图的fragment
 */
public class MyMapFragment extends SupportFragment implements LocationSource{

    private MapView mapView;
    private AMap aMap;
    private UiSettings mUiSettings;
    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption;
    private AMapLocationListener mLocationListener;
    private MyLocationStyle myLocationStyle;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(getActivity(),msg.toString(),Toast.LENGTH_SHORT).show();
        }
    };

    public static MyMapFragment newInstance() {
        return new MyMapFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.map_fragment,container,false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mapView=view.findViewById(R.id.map);
    }

    /**
     * 在此执行复杂操作：等待转场动画结束再执行操作，为的是防止动画卡顿
     * @param savedInstanceState
     */
    @Override
    public void onEnterAnimationEnd(Bundle savedInstanceState) {
        super.onEnterAnimationEnd(savedInstanceState);
        initMap(savedInstanceState);
        initLocationStyle();
        initLocation();
        initLocationParameter();
    }

    /**
     * 定位蓝点
     */
    private void initLocationStyle() {
        myLocationStyle=new MyLocationStyle();
        myLocationStyle.interval(2000);
        myLocationStyle.showMyLocation(true);
        aMap.setMyLocationStyle(myLocationStyle);
        aMap.setMyLocationEnabled(true);
    }

    /**
     * 配置定位
     */
    private void initLocation() {
        //声明定位回调监听器
        mLocationListener=new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if(aMapLocation.getErrorCode()!=0){
                    Message m=new Message();
                    m.obj="定位失败," + aMapLocation.getErrorCode()+ ": " + aMapLocation.getErrorInfo();
                    handler.sendMessage(m);
                }else{
                    Log.i("定位信息",aMapLocation.getProvince()+" "+aMapLocation.getCity()+" "+aMapLocation.getDistrict()+" "+
                            aMapLocation.getStreet()+" "+aMapLocation.getAoiName());
                }
                mLocationClient.stopLocation();
            }
        };
        mLocationClient=new AMapLocationClient(getActivity());
        mLocationClient.setLocationListener(mLocationListener);
    }

    /**
     * 配置定位参数并开启定位
     */
    private void initLocationParameter() {
        mLocationOption=new AMapLocationClientOption();
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setNeedAddress(true);
        mLocationClient.setLocationOption(mLocationOption);
        mLocationClient.startLocation();
    }

    /**
     * 初始化地图
     * @param savedInstanceState
     */
    private void initMap(Bundle savedInstanceState) {
        mapView.onCreate(savedInstanceState);//创建地图
        if(aMap==null){
            aMap=mapView.getMap();
        }
        aMap.setTrafficEnabled(true);
        aMap.showIndoorMap(true);
        aMap.setLocationSource(this);//通过aMap对象设置定位数据源的监听
        aMap.setMyLocationEnabled(true);
        mUiSettings = aMap.getUiSettings();
        mUiSettings.setCompassEnabled(true);
        mUiSettings.setMyLocationButtonEnabled(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();//销毁地图
        if(mLocationClient!=null){
            mLocationClient.onDestroy();//销毁定位对象
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();//重新绘制加载地图
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();//暂停地图的绘制
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);//保存地图当前的状态
    }

    @Override
    public void activate(LocationSource.OnLocationChangedListener onLocationChangedListener) {

    }

    @Override
    public void deactivate() {

    }

}
